# -*- coding: utf-8 -*-
# @Author : Ryhn
import os
import platform
import sys
from typing import Optional
import uvicorn
from fastapi import FastAPI, Query, Request, Body
from pydantic import BaseModel
from starlette.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

from service import EHtml

app = FastAPI(title="测试报告智能生成")
templates = Jinja2Templates(directory="templates/template")


class eemail(BaseModel):
     username: Optional[str] = Body(title="账户名", min_length=2, max_length=24)
     password: Optional[str] = Body(title="密码", min_length=1, max_length=40)
     sender:Optional[str] = Body(title="发送方邮箱", description="多收件人用逗号分割")

class Ai_model(BaseModel):
     appid: Optional[str] = Body(default="376a1324xuehaoye")
     api_secret: Optional[str] = Body(default="ZTJiMjMzYTAyMjdhZTMxZTJmNmNjMmY5xuehaoye")
     api_key: Optional[str] = Body(default="ae1f77a1c3cf0c116651dbf91b1d8aa7xuehaoye")

class test_result(BaseModel):
     result: Optional[str] = Body(title="测试结果")
     test: Optional[str] = Body(title="测试结论")

class Jira_model(BaseModel):
     url: Optional[str] = Body(title="jira或者其他项目管理平台", regex='[a-zA-Z:/]+', min_length=6)
     username: Optional[str] = Body(title="账户名", min_length=2, max_length=24)
     password: Optional[str] = Body(title="密码", min_length=1, max_length=40)
     key: Optional[str] = Body(title="问题单的key")

class reporter_model(BaseModel):
     msg: Optional[str] = Body(default="发送成功", title="报告生成情况反馈")

class reporter_request(BaseModel):
     email: eemail
     Ai: Ai_model
     jira: Jira_model
     test: test_result


@app.post(path='/newreporter', name="生成测试报告", response_model=reporter_model)
def new_reporter(request: reporter_request):
     '''
     新建
     :param request:
     :return:
     '''
     EHtml.main(jira_key=request.jira.key, jira_user=request.jira.username, jira_password=request.jira.password, jira_url=request.jira.url,
                result=request.test.result, text=request.test.test, api=request.Ai.appid, api_secret=request.Ai.api_secret, api_key=request.Ai.api_key,
                email_user=request.email.username, email_pass=request.email.password, receiver=request.email.sender)
     return {"msg": "Good Lucky"}

@app.get(path='/',response_class=HTMLResponse)
def home(request: Request):
     return templates.TemplateResponse("home.html", {"request":request})

if __name__=="__main__":
     uvicorn.run(app=app, host="0.0.0.0", port=8005)